package com.orhon.smartcampus.modules.student.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.dao.EclassHeadteacherDao;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.EclassHeadteacher;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLHeadTeacherInput;
import com.orhon.smartcampus.modules.student.inputMapper.HeadTeacherInputMapper;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.transaction.annotation.Transactional;

@Component
public class AddEclassHeadTeacherMutation implements DataFetcher {



    @Autowired
    EclassHeadteacherDao eclassHeadteacherDao;

    @Autowired
    EclassDao eclassDao;

    @Autowired
    UsersDao usersDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;


    @Autowired
    TokenService tokenService;


    /**
     * 设置班主任
     * editor : ths
     */
    @Override
    @Transactional(readOnly = false)
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token  = tokenService.getSystemToken();
        GQLHeadTeacherInput input = HeadTeacherInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        EclassHeadteacher eclassHeadteacher = null;
        if (input.getId() != null){
            eclassHeadteacher = eclassHeadteacherDao.get(EclassHeadteacher.class , input.getId());
            eclassHeadteacher.setUpdatedAt(new Date());
        }
        else{
            eclassHeadteacher = new EclassHeadteacher();
            eclassHeadteacher.setCreatedAt(new Date());
        }
        if(input.getEclass_id() != null) {
            eclassHeadteacher.setStudentEclassByEclassId(eclassDao.get(Eclass.class, input.getEclass_id()));
        }
        if(input.getUser_id() != null) {
            eclassHeadteacher.setUserUsersByUserId(usersDao.get(Users.class, input.getUser_id()));
        }
        if(input.getTeacher_id() != null) {
            eclassHeadteacher.setTeacherTInformationByTeacherId(tInformationDao.get(TInformation.class, input.getTeacher_id()));
        }
        if(input.getSchool_id() != null) {
            eclassHeadteacher.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class, input.getSchool_id()));
        }
        if(input.getStart_at() != null){

            eclassHeadteacher.setStartAt(input.getStart_at());
        }
        if(input.getEnd_at() != null){

            eclassHeadteacher.setEndAt(input.getEnd_at());
        }
        if(input.getStatus() != null){

            eclassHeadteacher.setStatus(input.getStatus());
        }
        if(input.getReason() != null){

            eclassHeadteacher.setReason(JSON.toJSONString(input.getReason()));
        }
        if(input.getTransaction() != null){

            eclassHeadteacher.setTransaction(input.getTransaction());
        }

        try{
            eclassHeadteacherDao.saveOrUpdate(eclassHeadteacher);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }

        return Result.ok().put("data",eclassHeadteacher);
    }
}

